﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Utility;
using Data;
namespace Website.UC
{
    public partial class UC_FAQ : System.Web.UI.UserControl
    {
        Tbl_FaqBus Faq_Bus = new Tbl_FaqBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
            }
        }
        public void loadData()
        {
            GridView1.DataSource = Faq_Bus.getListFaq();
            GridView1.DataBind();
        }
        public void clear()
        {
            txtTitle.Text = "";
            txtQuestion.Text = "";
            txtAnswer.Text = "";
        }

        protected void btnCancel_Click(object sender, System.EventArgs e)
        {
            clear();
        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            lblId.Text = WebHelper.GetValueGridView(GridView1, 2, e);
            txtTitle.Text = WebHelper.GetValueGridView(GridView1, 3, e);
            txtQuestion.Text = WebHelper.GetValueGridView(GridView1, 4, e);
            txtAnswer.Text = WebHelper.GetValueGridView(GridView1, 5, e);
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            tbl_faq tf = new tbl_faq();
            tf.faq_id = Int32.Parse(WebHelper.GetDataKey(GridView1, e));
            Faq_Bus.deleteFaq(tf);
            loadData();
            clear();
        }

        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            if (txtTitle.Text.Length == 0)
            {
                WebHelper.ShowMessage("Title is not null", Page);
            }
            if (txtQuestion.Text.Length == 0)
            {
                WebHelper.ShowMessage("Question is not null", Page);
            }
            if (txtAnswer.Text.Length == 0)
            {
                WebHelper.ShowMessage("Answer is not null", Page);
            }
            else
            {
                tbl_faq tf = new tbl_faq();
                tf.faq_title = txtTitle.Text;
                tf.question = txtQuestion.Text;
                tf.answer = txtAnswer.Text;
                if (lblId.Text.Length == 0)
                {
                    Faq_Bus.insertFaq(tf);
                }
                else
                {
                    tf.faq_id = Int32.Parse(lblId.Text);
                    Faq_Bus.updateFaq(tf);
                }
                clear();
                loadData();
            }
        }
    }
}